clc;
clear all;
close all;


%% load data
load 'TSEBpatches.mat'
b = length(blankPatches);
e = length(edgePatches);
s = length(structurePatches);
t = length(texturePatches);

%% save feature vectors


%% test var
figure;
b_var = zeros(b, 1);
for i = 1:b
    b_idx = blankPatches(i);
    b_img = PatchCell{b_idx};
    b_img = double(b_img);
    b_var(i) = var(b_img(:));
end
scatter(ones(b,1), b_var);hold on;
e_var = zeros(e, 1);
for i = 1:e
    e_idx = edgePatches(i);
    e_img = PatchCell{e_idx};
    e_img = double(e_img);
    e_var(i) = var(e_img(:));
end
scatter( 2 * ones(e,1), e_var);hold on;
s_var = zeros(s, 1);
for i = 1:s
    s_idx = structurePatches(i);
    s_img = PatchCell{s_idx};
    s_img = double(s_img);
    s_var(i) = var(s_img(:));
end
scatter(3 * ones(s,1), s_var);hold on;
t_var = zeros(t, 1);
for i = 1:t
    t_idx = texturePatches(i);
    t_img = PatchCell{t_idx};
    t_img = double(t_img);
    t_var(i) = var(t_img(:));
end
scatter(4 * ones(t,1), t_var);

%% test gm
figure;
b_gm = zeros(b, 1);
for i = 1:b
    b_idx = blankPatches(i);
    b_img = PatchCell{b_idx};
    b_img = double(b_img);
    gm_img = im_gradient(b_img);
    b_gm(i) = mean(gm_img(:));
end
e_gm = zeros(e, 1);
for i = 1:e
    e_idx = edgePatches(i);
    e_img = PatchCell{e_idx};
    e_img = double(e_img);
    gm_img = im_gradient(e_img);
    e_gm(i) = mean(gm_img(:));
end
s_gm = zeros(s, 1);
for i = 1:s
    s_idx = structurePatches(i);
    s_img = PatchCell{s_idx};
    s_img = double(s_img);
    gm_img = im_gradient(s_img);
    s_gm(i) = mean(gm_img(:));
end
t_gm = zeros(t, 1);
for i = 1:t
    t_idx = texturePatches(i);
    t_img = PatchCell{t_idx};
    t_img = double(t_img);
    gm_img = im_gradient(t_img);
    t_gm(i) = mean(gm_img(:));
end
scatter(ones(b,1), b_gm);hold on;
scatter(2 * ones(e,1), e_gm);hold on;
scatter(3 * ones(s,1), s_gm);hold on;
scatter(4 * ones(t,1), t_gm);

%% test var + gm
figure;
scatter(b_var, b_gm); hold on;
scatter(e_var, e_gm); hold on;
scatter(s_var, s_gm); hold on;
scatter(t_var, t_gm); hold on;
legend('Blank', 'Edge', 'Structure', 'Texture');

%% save feature vector
b_fv = mean([b_var/numel(b_img) b_gm]);
e_fv = mean([e_var/numel(b_img) e_gm]);
s_fv = mean([s_var/numel(b_img) s_gm]);
t_fv = mean([t_var/numel(b_img) t_gm]);

save('fv.mat', 'b_fv', 'e_fv', 's_fv', 't_fv');
fv = [b_fv; e_fv; s_fv; t_fv];
scatter(fv(:,1), fv(:,2));
% legend('Blank', 'Edge', 'Structure', 'Texture');
%% test var + gm normalized
min_var = min([b_var; e_var; s_var; t_var]);
max_var = max([b_var; e_var; s_var; t_var]);
min_gm = min([b_gm; e_gm; s_gm; t_gm]);
max_gm = max([b_gm; e_gm; s_gm; t_gm]);
b_var = (b_var - min_var)./(max_var - min_var);
e_var = (e_var - min_var)./(max_var - min_var);
s_var = (s_var - min_var)./(max_var - min_var);
t_var = (t_var - min_var)./(max_var - min_var);

b_gm = (b_gm - min_gm)./(max_gm - min_gm);
e_gm = (e_gm - min_gm)./(max_gm - min_gm);
s_gm = (s_gm - min_gm)./(max_gm - min_gm);
t_gm = (t_gm - min_gm)./(max_gm - min_gm);

figure;
scatter(b_var, b_gm); hold on;
scatter(e_var, e_gm); hold on;
scatter(s_var, s_gm); hold on;
scatter(t_var, t_gm); hold on;

%% test phase
close all;figure;

bk = randi(b, 1);
b_idx = blankPatches(bk);
b_img = PatchCell{b_idx};
b_img = double(b_img)/255;
b_fft = fftshift(fft2(b_img));
subplot(4,3, 1);imshow(b_img);
subplot(4,3, 2);imshow(log(abs(b_fft) + 1), []);
subplot(4,3, 3);imshow(angle(b_fft), []);

ek = randi(e, 1);
e_idx = edgePatches(ek);
e_img = PatchCell{e_idx};
e_img = double(e_img)/255;
e_fft = fftshift(fft2(e_img));
subplot(4,3, 4);imshow(e_img);
subplot(4,3, 5);imshow(log(abs(e_fft) + 1), []);
subplot(4,3, 6);imshow(angle(e_fft), []);

sk = randi(s, 1);
s_idx = structurePatches(sk);
s_img = PatchCell{s_idx};
s_img = double(s_img)/255;
s_fft = fftshift(fft2(s_img));
subplot(4,3, 7);imshow(s_img);
subplot(4,3, 8);imshow(log(abs(s_fft) + 1), []);
subplot(4,3, 9);imshow(angle(s_fft), []);

tk = randi(t, 1);
t_idx = texturePatches(tk);
t_img = PatchCell{t_idx};
t_img = double(t_img)/255;
t_fft = fftshift(fft2(t_img));
subplot(4,3, 10);imshow(t_img);
subplot(4,3, 11);imshow(log(abs(t_fft) + 1), []);
subplot(4,3, 12);imshow(angle(t_fft), []);

%% test mean phase
figure;
b_phase = zeros(b, 1);
for i = 1:b
    b_idx = blankPatches(i);
    b_img = PatchCell{b_idx};
    b_img = double(b_img)/255;
    fft = fftshift(fft2(b_img));
    phase = angle(fft);
    b_phase(i) = mean(phase(:));
end
scatter(ones(b,1), b_phase);hold on;
e_phase = zeros(e, 1);
for i = 1:e
    e_idx = edgePatches(i);
    e_img = PatchCell{e_idx};
    e_img = double(e_img)/255;
    fft = fftshift(fft2(e_img));
    phase = angle(fft);
    e_phase(i) = mean(phase(:));
end
scatter( 2 * ones(e,1), e_phase);hold on;
s_phase = zeros(s, 1);
for i = 1:s
    s_idx = structurePatches(i);
    s_img = PatchCell{s_idx};
    s_img = double(s_img)/255;
    fft = fftshift(fft2(s_img));
    phase = angle(fft);
    s_phase(i) = mean(phase(:));
end
scatter(3 * ones(s,1), s_phase);hold on;
t_phase = zeros(t, 1);
for i = 1:t
    t_idx = texturePatches(i);
    t_img = PatchCell{t_idx};
    t_img = double(t_img)/255;
    fft = fftshift(fft2(t_img));
    phase = angle(fft);
    t_phase(i) = mean(phase(:));
end
scatter(4 * ones(t,1), t_phase);

%% test DCT
[b_eh b_le b_leh] = ut_dct_leh_8585(PatchCell(blankPatches));
scatter(ones(t,1), b_eh);




